
#ifndef _STRING_FUNCTIONS_
#define _STRING_FUNCTIONS_
#include "Common/Definitions.h"

#include <string>
#include <string.h>
#include "Common/Math.h"
#include "Common/Errors.h"

namespace ttg
{
	namespace strings
	{

		//Creates copy of the given string.
		//Note: you must call delete[] for returned string.
		char *createCopy(const char *str);

		//Creates concatenation.
		//Note: you must call delete[] for returned string.
		char *createConcatenation(const char *str1,
								  const char *str2);

		//Casts single-char string to wide-char string.
		//Note: you must call delete[] for returned string.
		char *castToWide(const char *str);

		//Deletes space symbols from start and end of the string.
		std::string clearStartEndSpaces(const char *val);

		//Determinates, is it a int32 number.
		inline bool isInt(const char *s, ttg::math::int32 &val)
		{ return _snscanf_s(s, 1024, "%d", &val) > 0; }
		//Determinates, is it a int32 number.
		inline bool isInt(const std::string &s, ttg::math::int32 &val)
		{ return isInt(s.c_str(), val); }

		//Determinates, is it a int64 number.
		inline bool isInt(const char *s, ttg::math::int64 &val)
		{ return _snscanf_s(s, 1024, "%lld", &val) > 0; }
		//Determinates, is it int64 number.
		inline bool isInt(const std::string &s, ttg::math::int64 &val)
		{ return isInt(s.c_str(), val); }

		//Casts requred string to int32. In case of error throws ttg::Error().
		inline ttg::math::int32 toInt32(const char *s)
		{
			ttg::math::int32 res;
			if (!isInt(s, res))
				throw ttg::Error(ttg::Error::LOCAL, "strings", "toInt32",
								 "Given string does not contain int number");
			else
				return res;
		}
		//Casts requred string to int32. In case of error throws ttg::Error().
		inline ttg::math::int32 toInt32(const std::string &s)
		{ return toInt32(s.c_str()); }

		//Casts requred string to int64. In case of error throws ttg::Error().
		inline ttg::math::int64 toInt64(const char *s)
		{
			ttg::math::int64 res;
			if (!isInt(s, res))
				throw ttg::Error(ttg::Error::LOCAL, "strings", "toInt64",
								 "Given string does not contain int number");
			else
				return res;
		}
		//Casts requred string to int64. In case of error throws ttg::Error().
		inline ttg::math::int64 toInt64(const std::string &s)
		{ return toInt64(s.c_str()); }

		//Determinates, is it a float number.
		inline bool isFloat(const char *s, float &val)
		{
			//val = (float)atof(s);
			return _snscanf_s(s, 1024, "%f", &val) > 0;
		}
		//Determinates, is it a float number.
		inline bool isFloat(const std::string &s, float &val)
		{ return isFloat(s.c_str(), val); }

		//Determinates, is it a double number.
		inline bool isFloat(const char *s, double &val)
		{
			//val = atof(s);
			return _snscanf_s(s, 1024, "%lf", &val) > 0;
		}
		//Determinates, is it a double number.
		inline bool isFloat(const std::string &s, double &val)
		{ return isFloat(s.c_str(), val); }
	}
}

#endif
